<template>
  <div>
    <div v-show="visible && modal"
         class="dialog-modal"
         @click="handleClickModal"></div>
    <div v-show="visible"
         class="dialog-content"
         @click="handleClickModal">
      <div class="dialog-content-body"
           :style="bodyStyle"
           @click.stop>
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'CuiDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    modal: {
      type: Boolean,
      default: true
    },
    closeOnClickModal: {
      type: Boolean,
      default: true
    },
    showClose: {
      type: Boolean,
      default: true
    },
    bodyStyle: {
      type: String,
      default: null
    }
  },

  data () {
    return {}
  },
  watch: {
    visible (val) {
      if (!val) {
        this.$emit('closeDialog')
      }
    }
  },
  methods: {
    open () { this.visible = true },
    handleClickModal () {
      if (this.closeOnClickModal) {
        this.$emit('closeDialog')
      }
    }
  },

  mounted () {
    if (this.visible) {
      this.open()
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog-modal {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1030;
  background: #000000;
  opacity: 0.3;
}

.dialog-content {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1030;
  overflow: auto;

  &-body {
    position: absolute;
    left: 20%;
    top: 15vh;
    width: 60%;
    min-height: 60%;
    z-index: 1;
    background: #fff;
    padding: 20px;
    border: 1px solid #ddd;
    border-radius: 4px;
    margin: 60px 0;
  }
}
</style>
